package cn.edu.zjusc.Util;

import java.util.HashMap;
import java.util.List;

import cn.edu.zjusc.POJO.*;

/**
 * 
 * @author 毕伟伦 
 * 省市代码表缓存 
 * 该类由于考虑高效，没有做深拷贝保护措施 
 * 该类方法返回的List不可以进行再操作！
 */
public class CacheManager {

	private static List<ProvinceCode> provinces = null;
	private static HashMap<Integer, List<CityCode>> cities = new HashMap<Integer, List<CityCode>>();

	private static HashMap<Integer, List<UniversityCode>> universities = new HashMap<Integer, List<UniversityCode>>();
	
	private static List<DegreeTypeCode> degreeTypes=null;
		private static List<MajorCode> majors=null;
		
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<ProvinceCode> getProvinces() {
		if (provinces != null) {
			return provinces;
		} else {

			String hql="from ProvinceCode e";
			List cdList =PersistenceHelper.executeReader(hql);

			provinces=cdList;

			return cdList;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<CityCode>  getCityOfOneProvince (int id) {

		List<CityCode> trailList = cities.get(new Integer(id));
		// 缓存里有
		if (trailList != null) {
			return trailList;
		} else {
			String hql="from CityCode e where e.ci_provID=?1";
			Object[] params={id};
			List cdList =PersistenceHelper.executeReader(hql,params);

			cities.put(new Integer(id), cdList);
			return cdList;
		}

	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<UniversityCode> getUniversityInOneProvince(int id){

		List<UniversityCode> trailList = universities.get(new Integer(id));
		// 缓存里有
		if (trailList != null) {
			return trailList;
		} else {

		String hql="from UniversityCode e where e.unicd_provID=?1";
		Object[] params={id};
		List cdList =PersistenceHelper.executeReader(hql,params);

		universities.put(new Integer(id), cdList);

		return cdList;
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<DegreeTypeCode> getAllDegreeTypes() {

		if (degreeTypes != null) {
			return degreeTypes;
		} else {
			String hql="from DegreeTypeCode e";
			List cdList =PersistenceHelper.executeReader(hql);

			degreeTypes = cdList;

			return cdList;
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<MajorCode> getAllMajorInfo() {

		if (majors != null) {
			return majors;
		} else {

			String hql="from MajorCode e";
			List cdList =PersistenceHelper.executeReader(hql);
			
			majors = cdList;

			return cdList;
		}
	}

	public static void clearAll(){
		
	}
}
